$.Class("MonitorHyip.Grid",
// static properties
{     
    hyipID : 0,
    addParams : function(e, params)
    {
        e.sender.transport.options.read.data = params;
    }    
},
// prototype properties
    {
        // constructor function
        init: function(id) {
            this.id = id;
            
            this.dataSource = this.createDataSource();
            this.createGrid();     
            this.initEvents(id);
        },
        createDataSource: function()
        {
            var dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: "/hyip/getMonitor",
                        dataType: "json",
                        type: "post",
                        data : {
                            id : 0
                        }
                    }
                },
                pageSize: 25,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true,
                schema: {
                    data: "results",
                    total: function(data) {
                        return data.total;
                    }
                },
                requestStart: function(e) {
                    if(MonitorHyip.Grid.hyipID == 0)
                        return false;
            
                    var data = {
                        id : MonitorHyip.Grid.hyipID,
                        isAjax : true
                    };
                    
                    MonitorHyip.Grid.addParams(e, data);
                }
            });

            return dataSource;
        }, 
        createGrid : function()
        {
            $("#" + this.id).kendoGrid({
	        dataSource: this.dataSource,
	        //toolbar: ["Reset"],
	        //height: 500,
	        filterable: false,
	        sortable: false,
	        pageable: true,
	        columns: [{
                field:"grid_index",
                title:" ",
                width: 40
            },{
                field:"id",
                title:"ID",
                width: 40
            },{
                field:"domain",
                title:"Monitor  Domain",
                width: 170
            },{
            	title:"",
                field:"",         
                width: 65,
                template: '<a href="" onclick="return MonitorHyip.delete(${hyipID},${id});">Delete</a>'
            },{
            	title:"",
                field:"",                
            }/*,{ 
                command: "destroy", title: "", width: 100 }*/
            ],
            editable:false,
            sortable:false,
            selectable: 'row',
            dataBound: function(e) {                
            }
        });	
        },
        initEvents : function(id)
        {
            $(".k-grid-Reset", "#" + this.id).click(function(){
                $("#" + id).data("kendoGrid").dataSource.read();	// Reset grid
                return false;
            });	
        },
        refresh : function()
        {
            $("#" + this.id).data("kendoGrid").dataSource.read();	// Reset grid
        }
})

MonitorHyip.add = function()
{
    if(MonitorHyip.Grid.hyipID == 0)
        return false;
    
    $.ajax({
        type: 'post',
        url: '/hyip/addMonitor',
        data: {
            monitorID: $("#monitorDomain").val(),
            hyipID : MonitorHyip.Grid.hyipID
        },
        dataType: 'json',
        success: function(obj){
            if(obj.status == "success")
                $("#gridMonitor").data("kendoGrid").dataSource.read();
        },
    });
}

MonitorHyip.delete = function(hiypID, monitorID)
{
    if(confirm("Do you want to delete?"))
    {
        $.ajax({
            type: 'post',
            url: '/hyip/deleteMonitor',
            data: {
                monitorID: monitorID,
                hyipID : hiypID
            },
            dataType: 'json',
            success: function(obj){
                $("#gridMonitor").data("kendoGrid").dataSource.read();
            },
        });
    }
    
    return false;
}